home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src-glu / glu.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  8KB  |  324 lines

  1. /* $Id: glu.c,v 1.10 1998/04/22 00:35:50 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: glu.c,v $
  26.  * Revision 1.10  1998/04/22 00:35:50  brianp
  27.  * changed version to 3.0
  28.  *
  29.  * Revision 1.9  1997/12/09 03:03:32  brianp
  30.  * changed version to 2.6
  31.  *
  32.  * Revision 1.8  1997/10/04 01:30:20  brianp
  33.  * changed version to 2.5
  34.  *
  35.  * Revision 1.7  1997/08/13 01:25:21  brianp
  36.  * changed version string to 2.4
  37.  *
  38.  * Revision 1.6  1997/07/24 01:28:44  brianp
  39.  * changed precompiled header symbol from PCH to PC_HEADER
  40.  *
  41.  * Revision 1.5  1997/07/13 22:59:11  brianp
  42.  * added const to viewport parameter of gluPickMatrix()
  43.  *
  44.  * Revision 1.4  1997/05/28 02:29:38  brianp
  45.  * added support for precompiled headers (PCH), inserted APIENTRY keyword
  46.  *
  47.  * Revision 1.3  1997/04/12 16:19:02  brianp
  48.  * changed version to 2.3
  49.  *
  50.  * Revision 1.2  1997/03/11 00:58:34  brianp
  51.  * changed version to 2.2
  52.  *
  53.  * Revision 1.1  1996/09/27 01:19:39  brianp
  54.  * Initial revision
  55.  *
  56.  */
  57.  
  58.  
  59. #ifdef PC_HEADER
  60. #include "all.h"
  61. #else
  62. #include <math.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include "gluP.h"
  66. #endif
  67.  
  68.  
  69. /*
  70.  * Miscellaneous utility functions
  71.  */
  72.  
  73.  
  74. #ifndef M_PI
  75. #define M_PI 3.1415926536
  76. #endif
  77. #define EPS 0.00001
  78.  
  79.  
  80.  
  81.  
  82. void APIENTRY gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
  83.              GLdouble centerx, GLdouble centery, GLdouble centerz,
  84.              GLdouble upx, GLdouble upy, GLdouble upz )
  85. {
  86.    GLdouble m[16];
  87.    GLdouble x[3], y[3], z[3];
  88.    GLdouble mag;
  89.  
  90.    /* Make rotation matrix */
  91.  
  92.    /* Z vector */
  93.    z[0] = eyex - centerx;
  94.    z[1] = eyey - centery;
  95.    z[2] = eyez - centerz;
  96.    mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
  97.    if (mag) {  /* mpichler, 19950515 */
  98.       z[0] /= mag;
  99.       z[1] /= mag;
  100.       z[2] /= mag;
  101.    }
  102.  
  103.    /* Y vector */
  104.    y[0] = upx;
  105.    y[1] = upy;
  106.    y[2] = upz;
  107.  
  108.    /* X vector = Y cross Z */
  109.    x[0] =  y[1]*z[2] - y[2]*z[1];
  110.    x[1] = -y[0]*z[2] + y[2]*z[0];
  111.    x[2] =  y[0]*z[1] - y[1]*z[0];
  112.  
  113.    /* Recompute Y = Z cross X */
  114.    y[0] =  z[1]*x[2] - z[2]*x[1];
  115.    y[1] = -z[0]*x[2] + z[2]*x[0];
  116.    y[2] =  z[0]*x[1] - z[1]*x[0];
  117.  
  118.    /* mpichler, 19950515 */
  119.    /* cross product gives area of parallelogram, which is < 1.0 for
  120.     * non-perpendicular unit-length vectors; so normalize x, y here
  121.     */
  122.  
  123.    mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
  124.    if (mag) {
  125.       x[0] /= mag;
  126.       x[1] /= mag;
  127.       x[2] /= mag;
  128.    }
  129.  
  130.    mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
  131.    if (mag) {
  132.       y[0] /= mag;
  133.       y[1] /= mag;
  134.       y[2] /= mag;
  135.    }
  136.  
  137. #define M(row,col)  m[col*4+row]
  138.    M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
  139.    M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
  140.    M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
  141.    M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
  142. #undef M
  143.    glMultMatrixd( m );
  144.  
  145.    /* Translate Eye to Origin */
  146.    glTranslated( -eyex, -eyey, -eyez );
  147.  
  148. }
  149.  
  150.  
  151.  
  152. void APIENTRY gluOrtho2D( GLdouble left, GLdouble right,
  153.               GLdouble bottom, GLdouble top )
  154. {
  155.    glOrtho( left, right, bottom, top, -1.0, 1.0 );
  156. }
  157.  
  158.  
  159.  
  160. void APIENTRY gluPerspective( GLdouble fovy, GLdouble aspect,
  161.                   GLdouble zNear, GLdouble zFar )
  162. {
  163.    GLdouble xmin, xmax, ymin, ymax;
  164.  
  165.    ymax = zNear * tan( fovy * M_PI / 360.0 );
  166.    ymin = -ymax;
  167.  
  168.    xmin = ymin * aspect;
  169.    xmax = ymax * aspect;
  170.  
  171.    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
  172. }
  173.  
  174.  
  175.  
  176. void APIENTRY gluPickMatrix( GLdouble x, GLdouble y,
  177.                  GLdouble width, GLdouble height,
  178.                  const GLint viewport[4] )
  179. {
  180.    GLfloat m[16];
  181.    GLfloat sx, sy;
  182.    GLfloat tx, ty;
  183.  
  184.    sx = viewport[2] / width;
  185.    sy = viewport[3] / height;
  186.    tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
  187.    ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
  188.  
  189. #define M(row,col)  m[col*4+row]
  190.    M(0,0) = sx;   M(0,1) = 0.0;  M(0,2) = 0.0;  M(0,3) = tx;
  191.    M(1,0) = 0.0;  M(1,1) = sy;   M(1,2) = 0.0;  M(1,3) = ty;
  192.    M(2,0) = 0.0;  M(2,1) = 0.0;  M(2,2) = 1.0;  M(2,3) = 0.0;
  193.    M(3,0) = 0.0;  M(3,1) = 0.0;  M(3,2) = 0.0;  M(3,3) = 1.0;
  194. #undef M
  195.  
  196.    glMultMatrixf( m );
  197. }
  198.  
  199.  
  200.  
  201. const GLubyte* APIENTRY gluErrorString( GLenum errorCode )
  202. {
  203.    static char *tess_error[] = {
  204.       "missing gluEndPolygon",
  205.       "missing gluBeginPolygon",
  206.       "misoriented contour",
  207.       "vertex/edge intersection",
  208.       "misoriented or self-intersecting loops",
  209.       "coincident vertices",
  210.       "colinear vertices",
  211.       "intersecting edges",
  212.       "not coplanar contours"
  213.    };
  214.    static char *nurbs_error[] = {
  215.       "spline order un-supported",
  216.       "too few knots",
  217.       "valid knot range is empty",
  218.       "decreasing knot sequence knot",
  219.       "knot multiplicity greater than order of spline",
  220.       "endcurve() must follow bgncurve()",
  221.       "bgncurve() must precede endcurve()",
  222.       "missing or extra geometric data",
  223.       "can't draw pwlcurves",
  224.       "missing bgncurve()",
  225.       "missing bgnsurface()",
  226.       "endtrim() must precede endsurface()",
  227.       "bgnsurface() must precede endsurface()",
  228.       "curve of improper type passed as trim curve",
  229.       "bgnsurface() must precede bgntrim()",
  230.       "endtrim() must follow bgntrim()",
  231.       "bgntrim() must precede endtrim()",
  232.       "invalid or missing trim curve",
  233.       "bgntrim() must precede pwlcurve()",
  234.       "pwlcurve referenced twice",
  235.       "pwlcurve and nurbscurve mixed",
  236.       "improper usage of trim data type",
  237.       "nurbscurve referenced twice",
  238.       "nurbscurve and pwlcurve mixed",
  239.       "nurbssurface referenced twice",
  240.       "invalid property",
  241.       "endsurface() must follow bgnsurface()",
  242.       "misoriented trim curves",
  243.       "intersecting trim curves",
  244.       "UNUSED",
  245.       "unconnected trim curves",
  246.       "unknown knot error",
  247.       "negative vertex count encountered",
  248.       "negative byte-stride encounteed",
  249.       "unknown type descriptor",
  250.       "null control array or knot vector",
  251.       "duplicate point on pwlcurve"
  252.    };
  253.  
  254.    /* GL Errors */
  255.    if (errorCode==GL_NO_ERROR) {
  256.       return (GLubyte *) "no error";
  257.    }
  258.    else if (errorCode==GL_INVALID_VALUE) {
  259.       return (GLubyte *) "invalid value";
  260.    }
  261.    else if (errorCode==GL_INVALID_ENUM) {
  262.       return (GLubyte *) "invalid enum";
  263.    }
  264.    else if (errorCode==GL_INVALID_OPERATION) {
  265.       return (GLubyte *) "invalid operation";
  266.    }
  267.    else if (errorCode==GL_STACK_OVERFLOW) {
  268.       return (GLubyte *) "stack overflow";
  269.    }
  270.    else if (errorCode==GL_STACK_UNDERFLOW) {
  271.       return (GLubyte *) "stack underflow";
  272.    }
  273.    else if (errorCode==GL_OUT_OF_MEMORY) {
  274.       return (GLubyte *) "out of memory";
  275.    }
  276.    /* GLU Errors */
  277.    else if (errorCode==GLU_NO_ERROR) {
  278.       return (GLubyte *) "no error";
  279.    }
  280.    else if (errorCode==GLU_INVALID_ENUM) {
  281.       return (GLubyte *) "invalid enum";
  282.    }
  283.    else if (errorCode==GLU_INVALID_VALUE) {
  284.       return (GLubyte *) "invalid value";
  285.    }
  286.    else if (errorCode==GLU_OUT_OF_MEMORY) {
  287.       return (GLubyte *) "out of memory";
  288.    }
  289.    else if (errorCode==GLU_INCOMPATIBLE_GL_VERSION) {
  290.       return (GLubyte *) "incompatible GL version";
  291.    }
  292.    else if (errorCode>=GLU_TESS_ERROR1 && errorCode<=GLU_TESS_ERROR9) {
  293.       return (GLubyte *) tess_error[errorCode-GLU_TESS_ERROR1];
  294.    }
  295.    else if (errorCode>=GLU_NURBS_ERROR1 && errorCode<=GLU_NURBS_ERROR37) {
  296.       return (GLubyte *) nurbs_error[errorCode-GLU_NURBS_ERROR1];
  297.    }
  298.    else {
  299.       return NULL;
  300.    }
  301. }
  302.  
  303.  
  304.  
  305. /*
  306.  * New in GLU 1.1
  307.  */
  308.  
  309. const GLubyte* APIENTRY gluGetString( GLenum name )
  310. {
  311.    static char *extensions = "";
  312.    static char *version = "1.1 Mesa 3.0";
  313.  
  314.    switch (name) {
  315.       case GLU_EXTENSIONS:
  316.      return (GLubyte *) extensions;
  317.       case GLU_VERSION:
  318.      return (GLubyte *) version;
  319.       default:
  320.      return NULL;
  321.    }
  322. }
  323.  
  324.